package alertatics.prototipe;

import org.json.JSONException;
import org.json.JSONObject;

import alertatics.prototipe.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.funtionality.FuncionesUsuario;
import android.funtionality.SHA1;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WinValidarUsr extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		RelativeLayout rel = (RelativeLayout) findViewById(id.layout01);
		rel.setBackgroundColor(Color.BLACK);

		ImageButton botonOk = (ImageButton) findViewById(id.btnOk);
		botonOk.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				final FuncionesUsuario funciones = new FuncionesUsuario();

				final String phone = ((TextView) findViewById(R.id.txtPhone))
						.getText().toString();
				final String password = ((TextView) findViewById(id.txtClave))
						.getText().toString();
				final String pais = ((TextView) findViewById(id.txtPais))
						.getText().toString();
				Thread t = new Thread(new Runnable() {

					public void run() {
						JSONObject json = funciones.validateUser(phone,
								SHA1.sha1Hash(password), pais,
								SHA1.sha1Hash(phone + "&" + pais));

						try {
							if (json != null
									&& json.getString("success") != null) {
								String res = json.getString("success");
								if (Integer.parseInt(res) == 1) {
									Object usuario = json
											.get("USRCODE");
									Intent i = new Intent(WinValidarUsr.this,
											WinBigButton.class);
									startActivity(i);
								} else {
									Toast.makeText(getApplicationContext(),
											"Identificacion fallida",
											Toast.LENGTH_SHORT).show();
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
				t.start();

			}
		});

		ImageButton botoncancel = (ImageButton) findViewById(id.btnCancel);
		botoncancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
				System.exit(0);
			}
		});
	}

}
